import { Controller } from 'egg';

export default class GoodsController extends Controller {
  async list() {
    const { query } = this.ctx.validateReq('goods.list');
    const { page, pageSize, ...where } = query;

    const list = await this.ctx.service.goods.main.list(page, pageSize, where);

    this.ctx.success({
      data: list,
    });
  }


  async create() {
    const { body } = this.ctx.validateReq('goods.create');
    const result = await this.ctx.service.goods.main.create(body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
        status: 201,
      });
    }
  }

  async update() {
    const { body, params: { id } } = this.ctx.validateReq('goods.update');
    const result = await this.ctx.service.goods.main.update(id, body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
      });
    }
  }

  async goodsByCategoryId() {
    const { query, params: { id } } = this.ctx.validateReq(
      'xxm.category.goods.id',
    );
    const { page, pageSize } = query;

    const result = await this.ctx.service.goods.main.goodsByCategory(page, pageSize, id)

    if (result) {
      this.ctx.success({
        data: result
      });
    }
  }




  async detail() {
    const { query } = this.ctx.validateReq('goods.detail');
    const data = await this.ctx.service.goods.main.detail(query);

    if (data) {
      this.ctx.success({
        data,
      });
    }
  }


  async goodsDestroy() {
    const { params: { id } } = this.ctx.validateReq('goods.gallery');
    const data = await this.ctx.model.GoodsGallery.destroy({ where: { id } });
    if (data) {
      this.ctx.success({
        data,
      });
    }
  }

  async attrval() {
    const { params: { id: goods_id }, body } = this.ctx.validateReq('goods.list.attrval');
    const { goods_attr, attr_val } = body;
    const baseWhere = { goods_id };

    const attr_val_list: any = [];

    // 新增商品关联属性
    const attr_list = attr_val.map(it => {
      const { attr_id, attr_name } = it;
      return {
        attr_id,
        attr_name,
        goods_id
      };
    });

    await this.ctx.model.GoodsAttr.destroy({ where: baseWhere });
    await this.ctx.model.GoodsAttrVal.destroy({ where: baseWhere });
    const attrIds = await this.ctx.model.GoodsAttr.bulkCreate(attr_list);

    attr_val.forEach(it => {
      const { attr_id, vals } = it;
      const attr = attrIds.find((attrId: any) => attrId.attr_id === attr_id) || {}
      // 属性-值
      vals.forEach(item => {
        const { id, name } = item;
        attr_val_list.push({
          attr_key_id: id,
          attr_id: attr.id,
          goods_id,
          attr_key_name: name
        })
      });
    });

    await this.ctx.model.GoodsAttrVal.bulkCreate(attr_val_list);

    // 新增sku
    const sku_list = goods_attr.map(it => ({
      ...it,
      ...baseWhere
    }));
    await this.ctx.model.GoodsSku.destroy({ where: baseWhere });
    const data = await this.ctx.model.GoodsSku.bulkCreate(sku_list);
    if (data) {
      this.ctx.success({
        data,
      });
    }

  }


  // 删除商品
  async destroy() {
    const { params: { id } } = this.ctx.validateReq('goods.destroy');

    await this.ctx.sassmodel.MarketGoods.destroy({
      where: {
        goods_id: id
      }
    });
    const result = await this.ctx.service.goods.main.destroy(id);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        }
      });
    }
  }

}
